package alarm.ruiqi.com.alarm_pro.service;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Environment;

import com.ab.task.thread.AbTaskPool;
import com.ab.task.thread.AbTaskQueue;
import com.ab.util.AbFileUtil;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.iflytek.cloud.SpeechUtility;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.util.LogUtils;

import java.util.HashMap;
import java.util.Map;

import alarm.ruiqi.com.alarm_pro.utils.CacheUtils;
import alarm.ruiqi.com.alarm_pro.utils.Constant;


public class MobileApplication extends Application {

    public static final String TAG = "com.alarm_pro";
    public static CacheUtils cacheUtils;
    private Map<String, Context> taskCtxMap = new HashMap<>();
    private static MobileApplication mobileApplication;
    private AbTaskQueue queue;
    private LocationService mLocatioService = null;
    @Override
    public void onCreate() {
        super.onCreate();
        mobileApplication = this;
        cacheUtils = CacheUtils.get(this, TAG + ".cache");
        LogUtils.customTagPrefix = "tag_print";
        SpeechUtility utility =  SpeechUtility.createUtility(this, "appid=" + Constant.SPEECH_APP_ID);
        AbFileUtil.initFileDir(this);
        //LogUtils.i("init OK!!!!"+utility.toString());


       mLocatioService = new LocationService(this, new BDLocationListener() {
            @Override
            public void onReceiveLocation(BDLocation bdLocation) {

                if (LocationService.isLocationOK(bdLocation.getLocType())){
                    LocationService.Location_City = bdLocation.getCity();
                    LocationService.Location_District = bdLocation.getDistrict();
                }else {
                    LocationService.Location_City ="";
                    LocationService.Location_District ="";
                }
                mLocatioService.stopLocation();
            }
        });
        mLocatioService.startLocation();

        //初始化基础数据
     new Thread(new Runnable() {
         @Override
         public void run() {
             TabsServiceImpl servie = new TabsServiceImpl(getInstance());
                 servie.initTabData();
                 LogUtils.i("初始化数据成功");
         }
     }).start();

    }

    public static synchronized MobileApplication getInstance() {
        return mobileApplication;
    }


    /*=================== exit ============================*/
    public void exitApp() {
        for (Context ctx : taskCtxMap.values()) {
            if (ctx instanceof Activity) {
                ((Activity) ctx).finish();
            }
        }
        String packName = this.getPackageName();
        ((ActivityManager) getSystemService(ACTIVITY_SERVICE)).restartPackage(packName);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    /*=================== taskCtxMap operate ============================*/
    public void putCtx(Context ctx) {
        taskCtxMap.put(ctx.getClass().getSimpleName(), ctx);
    }

    public Context getCtx(String key) {
        return taskCtxMap.get(key);
    }

    public void removeCtx(Context context) {
        taskCtxMap.remove(context);
    }
     /*=================== taskCtxMap operate ============================*/

    public boolean isSDCardMounted() {
        return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
    }


    public DbUtils getDefaultDbutils(){
        DbUtils db =   DbUtils.create(this, AbFileUtil.getDbDownloadDir(this), "alarm.db");
        db.configAllowTransaction(true);
        db.configDebug(true);
        return  db;

    }

    public AbTaskQueue getQueue() {
        if(queue == null)
            queue = AbTaskQueue.newInstance();
        return queue;
    }

    public AbTaskPool getPool() {
        return AbTaskPool.getInstance();
    }

    public LocationService getLocatioService() {
        return mLocatioService;
    }
}
